package ee.serge.client.content;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import ee.serge.client.Move;
import ee.serge.service.Service;
import ee.serge.shared.Feedback;
import ee.serge.shared.Loan;
import ee.serge.util.Calculator;
import ee.serge.util.Content;
import ee.serge.util.FormPanel;
import ee.serge.util.Headers;
import ee.serge.util.RatingPanel;
import ee.serge.util.SimpleCallback;
import ee.serge.util.URLData;
import ee.serge.util.bootstrap.BButton;
import ee.serge.util.bootstrap.BTable;

public class LoanDetailsContent extends Content {

   private static final String NAME = "loan";
   
   public static String getName() {
      return NAME;
   }
   
   public static URLData getURLData(long id) {
      URLData data = new URLData();
      data.setName(NAME);
      data.add("id", id);
      return data;
   }
   
   public static void show() {
      long id = Long.valueOf(Move.get().getCurrentURLData().get("id"));
      Service.getPublicService().getLoan(id, new SimpleCallback<Loan>() {

         @Override
         public void success(Loan result) {
            Move.get().setContent(new LoanDetailsContent(result));
         }
         
      });
   }

   public LoanDetailsContent(Loan result) {
      buildPanel(result);
   }
   
   private DockPanel dock;
   private VerticalPanel fastPanel;
   
   private void buildPanel(final Loan loan) {
      dock = new DockPanel();
      fastPanel = new VerticalPanel();
      dock.add(new Image(loan.getImageLink()), DockPanel.WEST);
      dock.add(fastPanel, DockPanel.EAST);
      
      fastPanel.addStyleName("margin-left");
      fastPanel.add(Headers.h2(loan.getName()));
      fastPanel.add(new Label(loan.getComment()));
      fastPanel.add(new RatingPanel((float)(loan.getTotalRating()) / loan.getFeedbackCount(), false));
      
      add(dock);

      FormPanel panel = new FormPanel();
      panel.add("Link", loan.getLink());
      panel.add("APR", loan.getAPR() + "%");
      panel.add("Transfer fee", loan.getTransferFee() + "£");
      panel.add("Longest period", loan.getPeriod() + " days");
      panel.add("Minimum loan", loan.getMinLoan() + "£");
      panel.add("Maximum loan", loan.getMaxLoan() + "£");
      fastPanel.add(panel);
      
      add(Headers.subheader("Customer Reviews"));
      
      String[] headers = {"#", "Rating", "Review"};
      BTable reviewTable = new BTable(loan.getFeedbackList().size(), headers);
      for (int i = 0; i < loan.getFeedbackList().size(); i++) {
         Feedback feedback = loan.getFeedbackList().get(i);
         reviewTable.setText(i, 0, i + 1);
         reviewTable.setWidget(i, 1, new RatingPanel(feedback.getRating(), false));
         reviewTable.setText(i, 2, feedback.getComment());
      }
      
      add(reviewTable);

      add(new BButton("Write your own review", BButton.PRIMARY, new ClickHandler() {
         @Override
         public void onClick(ClickEvent event) {
            AddReviewPopup.show(loan.getKey());
         }
      }));
      
      if (LoansRequester.isInitialised()) {
         HTML header = Headers.subheader("Calculator");
         header.addStyleName("margin-top");
         add(header);
         add(new Calculator(false));
      }
   }
   
}
